package SEApp.dialogs {
	import SEApp.boxes.albums.se_userAvatar;
	import SEApp.se_util;
	
	import UIFree.controls.uif_hscrollArea;
	import UIFree.uif_component;
	
	import flash.events.Event;
	import flash.geom.Rectangle;

	public class se_friendsDialog extends se_dialog {
		private const WIDTH: int = 550;
		private const HEIGHT: int = 380;
		private var _doSelect: Function;
		protected var _area: uif_hscrollArea; 
		
		public function se_friendsDialog(parent:uif_component, canClose: int=1, 
										a_classStyle: String='items', mainButtonCaption: String='', 
										mainItemObject: Object=null) {
			super(parent, 0, 0, WIDTH, HEIGHT, true, canClose);
			createComponents(a_classStyle, mainButtonCaption, mainItemObject);
		}
		
		protected function get doSelect(): Function {
			return _doSelect;
		}
		
		protected function createComponents(a_classStyle: String='items', mainButtonCaption: String ='', mainItemObject: Object=null): void {
			var inner: Rectangle = listRect;
			if (mainButtonCaption) {
				inner.height -= se_util.DEFHEIGHTBUTTON;
				se_util.defButton(this, 0, inner.height, mainButtonCaption, function(e: Event): void {
					 _doSelect(mainItemObject);
				});
			}
			_area = new uif_hscrollArea(this, inner.x, inner.y, inner.width, inner.height, a_classStyle, 30);
			_area.addEvent(Event.SELECT, doSelectUser);
			
			title.text = 'Друзья';
			(app as Object).externalServer.getAllFriends((app as Object).externalServer.viewer_id, function(data: *): void {
				doResponseFriend(data);
			}, 'getAllFriends');
		}
		
		protected function get listRect(): Rectangle {
			return getInner();
		}
		
		public function get area(): uif_hscrollArea {
			return _area;
		}
		
		public function showSelect(a_selectFunc: Function): void {
			_doSelect = null;
			_area.selectIndex = -1;
			show();
			_doSelect = a_selectFunc;
		}
		
		protected function doSelectUser(e: Event): void {
			if ((_doSelect != null) && (_area.selectIndex > -1)) _doSelect(_area.items[_area.selectIndex].object);
		}
		
		public function doResponseFriend(data:*): void {
			_area.assignList(data, _area.calcFrameWidth(3), function(_scroll: uif_component, inner: Rectangle, index: int): se_userAvatar {
				return new se_userAvatar(_scroll, inner.x, inner.y, inner.width, inner.height, data[index], _area.style['unselect']);   
			}, 3);
		}
	}
}